//
//  XD_GoalCertItemCell.swift
//  XueDao
//
//  Created by wenwen on 2021/3/18.
//

import UIKit

class XD_GoalCertItemCell: UICollectionViewCell {
    
    var _model : XS_CourseItemModel!
    public var model : XS_CourseItemModel!{
        set{
            _model = newValue
            //设置数据
            statusLabel.text = newValue.percentage == "100" ? "已学完" : ("学习中"+newValue.percentage + "%")
            statusView.backgroundColor = newValue.percentage == "100" ? UIColor.qmui_color(withHexString: "#C4C6CD") : UIColor.qmui_color(withHexString: "#D4F2EB")
            statusLabel.textColor = newValue.percentage == "100" ? UIColor.qmui_color(withHexString: "#ffffff") : UIColor.qmui_color(withHexString: "#2ABD9C")
            titleLabel.text = newValue.curriculumName
            contentLabel.text = "共" + newValue.curriculumNum + "节 | 预计学习\(newValue.planDay)天"
            imgView.sd_setImage(with: URL.init(string: newValue.curriculumImgUrl), completed: nil)
        }
        get{
            return _model
        }
    }
    
    lazy var backView: UIView = {
        let backView = UIView()
        
        let backVW = CGFloat(.screenWidth - 32)
        let backVH = CGFloat(.scale * 104)
        let layerColor = UIColor(red: 0.97, green: 0.97, blue: 0.97, alpha: 1.00).cgColor
        
        let leftLayer = CALayer()
        leftLayer.frame = CGRect.init(x: 0, y: 0, width: 1, height: backVH)
        leftLayer.backgroundColor = layerColor
        let rightLayer = CALayer()
        rightLayer.frame = CGRect.init(x: 0, y: backVH - 1, width: backVW, height: 1)
        rightLayer.backgroundColor = layerColor
        let bottomLayer = CALayer()
        bottomLayer.frame = CGRect.init(x: backVW - 1, y: 0, width: 1, height: backVH)
        bottomLayer.backgroundColor = layerColor
        
        backView.layer.addSublayer(leftLayer)
        backView.layer.addSublayer(rightLayer)
        backView.layer.addSublayer(bottomLayer)
        
        return backView
    }()
    
    lazy var imgView: UIImageView = {
        let imgView = UIImageView()
        imgView.layer.masksToBounds = true
        imgView.layer.cornerRadius = .scale * 4
        
        return imgView
    }()
    
    lazy var statusView: UIView = {
        let statusView = UIView()
        statusView.layer.masksToBounds = true
        statusView.layer.cornerRadius = .scale * 2
        
        return statusView
    }()
    
    lazy var statusLabel: UILabel = {
        let statusLabel = UILabel()
        statusLabel.font = .systemFont(ofSize: .scale * 10, weight: .medium)
        statusLabel.textAlignment = .center
        return statusLabel
    }()
    
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = .systemFont(ofSize: .scale * 16, weight: .medium)
        titleLabel.textColor = UIColor(red: 0.22, green: 0.24, blue: 0.25, alpha: 1.00)
        titleLabel.textAlignment = .left
        return titleLabel
    }()
    
    lazy var contentLabel: UILabel = {
        let contentLabel = UILabel()
        contentLabel.font = .systemFont(ofSize: .scale * 12, weight: .regular)
        contentLabel.textColor = UIColor(red: 0.67, green: 0.69, blue: 0.72, alpha: 1.00)
        contentLabel.textAlignment = .left
        return contentLabel
    }()
    
    lazy var goStudyBtn: UIButton = {
        let goStudyBtn = UIButton()
        goStudyBtn.setTitle("去学习", for: .normal)
        goStudyBtn.setTitleColor(UIColor(red: 0.16, green: 0.74, blue: 0.61, alpha: 1.00), for: .normal)
        goStudyBtn.layer.borderColor = UIColor(red: 0.16, green: 0.74, blue: 0.61, alpha: 1.00).cgColor
        goStudyBtn.layer.masksToBounds = true
        goStudyBtn.layer.borderWidth = 1
        goStudyBtn.layer.cornerRadius = .scale * 12
        
        goStudyBtn.titleLabel?.font = .systemFont(ofSize: .scale * 12, weight: .medium)
        goStudyBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: .scale * 12, bottom: 0, right: .scale * 12)
        goStudyBtn.isEnabled = false
        
       return goStudyBtn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        backView.addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.left.equalTo(.scale * 12)
            make.centerY.equalToSuperview()
            make.width.equalTo(.scale * 112)
            make.height.equalTo(.scale * 80)
        }
        
        statusView.addSubview(statusLabel)
        statusLabel.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(.scale * 5)
            make.right.equalTo(-.scale * 5)
        }
        
        imgView.addSubview(statusView)
        statusView.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview()
            make.height.equalTo(.scale * 18)
        }
        
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(imgView)
            make.left.equalTo(imgView.snp.right).offset(.scale * 12)
            make.right.equalTo(-.scale * 12)
            make.height.equalTo(.scale * 22)
        }
        
        backView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
            make.left.right.equalTo(titleLabel)
        }
        
        backView.addSubview(goStudyBtn)
        goStudyBtn.snp.makeConstraints { (make) in
            make.right.equalTo(-.scale * 12)
            make.height.equalTo(.scale * 26)
            make.width.equalTo(.scale * 62)
            make.bottom.equalTo(imgView)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
